package cn.itcast.vod.controller;

import cn.itcast.baseservice.exceptionhandle.GuliException;
import cn.itcast.commonutils.R;
import cn.itcast.vod.service.VodService;
import cn.itcast.vod.utils.ConstantVodUtils;
import cn.itcast.vod.utils.InitVodCilent;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description = "视频管理")
@RestController
@CrossOrigin
@RequestMapping("/eduvod/video")
public class VodController {
    @Autowired
    private VodService vodService;

    @ApiOperation(value = "文件(视频)上传")
    @PostMapping("upload")
    public R uploadVideoAliyun(MultipartFile file){
        String videoId = vodService.uploadVideoAliyun(file);

        return R.ok().data("videoId",videoId);
    }

    // 根据视频id删除阿里云视频
    @DeleteMapping("removeAliyunVideo/{id}")
    public R removeAliyunVideo(@PathVariable String id){
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(id);
            // 删除
            client.getAcsResponse(request);

            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();

            throw new GuliException(20001,"删除视频失败");
        }
    }

    // 根据视频id，删除多个视频
    @DeleteMapping("delete-batch")
    public R removeMoreAliyunVideo(@RequestParam List<String> ids){
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();

            if (ids.size() > 0) {
                String idsStr = StringUtils.join(ids.toArray(),",");

                //支持传入多个视频ID，多个用逗号分隔
                request.setVideoIds(idsStr);
                // 删除
                client.getAcsResponse(request);
            }

            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();

            throw new GuliException(20001,"删除视频失败");
        }
    }

    // 根据视频id获取播放凭证
    @ApiOperation(value = "获取播放凭证")
    @GetMapping("/getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable String videoId){
        try {
            // 创建初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient("LTAI4G8fKqEgygE1WzHsiVDm", "AR2ppon4fdsi7c7H67CSoax60NJHpe");

            // 创建获取视频凭证request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            // 向request设置视频id
            request.setVideoId(videoId);

            // 调用初始化对象的方法得到凭证
            response = client.getAcsResponse(request);

            return R.ok().data("playAuth",response.getPlayAuth());
        } catch (Exception e) {
            return R.error().message("获取播放凭证失败!");
        }
    }
}
